<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 控件数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;

class WidgetController extends VerifyController
{
    /**
     * 获取模块类型列表
     * @return \think\Response
     */
    public function getModuleTypeList()
    {
        $moduleTypeList = [
            ['id' => 'entity', 'name' => 'Entity'],
            ['id' => 'fixed', 'name' => 'Fixed']
        ];
        return json($moduleTypeList);
    }

    /**
     * 获取当前行前后项数据
     * @param $moduleType
     * @param $param
     * @return array
     */
    protected function getModuleItemPrevAndNext($moduleType, $param)
    {
        if ($moduleType === "entity") {
            $class = '\\common\\model\\EntityModel';
            $prevFilter = [
                "module_id" => $param["module_id"],
                "project_id" => $param["project_id"],
                "id" => ["LT", $param["item_id"]]
            ];
            $nextFilter = [
                "module_id" => $param["module_id"],
                "project_id" => $param["project_id"],
                "id" => ["GT", $param["item_id"]]
            ];
        } else {
            $class = '\\common\\model\\' . string_initial_letter($param["module_code"]) . 'Model';
            $prevFilter = [
                "id" => ["LT", $param["item_id"]]
            ];
            $nextFilter = [
                "id" => ["GT", $param["item_id"]]
            ];
        }
        $serviceObj = new $class();

        $prevId = $serviceObj->where($prevFilter)->order("id desc")->limit(1)->getField("id");
        $nextId = $serviceObj->where($nextFilter)->order("id asc")->limit(1)->getField("id");

        $prevOne = $prevId ? $prevId : 0;
        $nextOne = $nextId ? $nextId : 0;

        return ["prev" => $prevOne, "next" => $nextOne];
    }
}
